﻿using Ldt.DataEngine.RuleAssemble;
using LdtJudge.DataEngine.Common.CommHelp;
using LdtJudge.DataEngine.Model.DataModel.SysCommon;
using System;
using System.Collections.Generic;
using System.Linq;

namespace LdtDataEngin.ModuleDetail.AnalysisModule.CaseCauseBaseInfoExtr
{
    /// <summary>
    /// 案由信息提取
    /// Class A2CaseCauseExt.
    /// </summary>
    public class A2CaseCauseExt : ACaseCauseExt
    {
        /// <summary>
        /// 返回案由的集合
        /// Cases the cause extract.
        /// </summary>
        /// <param name="caseCause">The case cause.</param>
        /// <param name="OriginalText">原文</param>
        /// <param name="docID"></param>
        /// <param name="caseType">案件类型</param>
        /// <returns>IList&lt;CaseCauseModel&gt;.</returns>
        public IList<CaseCauseModel> CaseCauseExtract(string caseCause,string OriginalText, string docID,string caseType)
        {
            // 刑事罪行列表
            IList<Tuple<string, string>> crimeNameList = new List<Tuple<string, string>>();
            IList<CaseCauseModel> reList = new List<CaseCauseModel>();
            GetSysCaseCause dalCause = new GetSysCaseCause();
            string MacthKeyWord = string.Empty;

            if (string.IsNullOrEmpty(caseCause))
            {
                /// 如果案由为空，利用正则获取案由信息
                // 从数据库中获取刑事类型案件的正则编号
                string regexInfoCode = "XSCaseCauseID";
                if (!string.IsNullOrEmpty(regexInfoCode))
                {
                    // 传入原文执行正则获取案由
                    var rowList = RuleParseClass.GetParseRegCodeList(regexInfoCode, new List<string> { OriginalText });
                    foreach (string causeName in rowList)
                    {
                        // 案由名称处理，确保案由名称有且仅有一个“罪”字，
                        string newCauseName = causeName;
                        newCauseName = (causeName + "罪").Replace("罪罪", "罪");

                        //对提取过的罪名，不再做二次提取。
                        if (crimeNameList.Any(tup => tup.Item1 == newCauseName))
                            continue;
                        else
                            crimeNameList.Add(new Tuple<string, string>(newCauseName, OriginalText));
                    }
                }
                
            }
            else
            {
                if (caseCause.IndexOf(",") > -1)
                {
                    foreach (string cause in caseCause.Split(','))
                    {
                        crimeNameList.Add(new Tuple<string, string>(cause, "平台传入的案由，不进行案由提取！"));
                    }
                }
                else
                {
                    crimeNameList.Add(new Tuple<string, string>(caseCause, "平台传入的案由，不进行案由提取！"));

                }
            }

            // 遍历获取到的案由与原文的tuple
            foreach (Tuple<string, string> causeName in crimeNameList)
            {
                //根据案由名称获取案由数据
                var cause = dalCause.GetCauseByName(causeName.Item1);
                if(cause!=null)
                {
                    if(cause.CC_NodeType==2)
                    {
                        cause.MatchOrginalText = causeName.Item2;
                        reList.Add(cause);
                    }
                    else
                    {
                        //获取父级Id
                        var allParentId = cause.AllParentId;
                        if(!string.IsNullOrWhiteSpace(allParentId))
                        {
                            var parentIdList = allParentId.Split(',').Reverse().ToList();

                            foreach (var id in parentIdList)
                            {
                                var currentCause = dalCause.GetCauseById(id.Trim());
                                if(currentCause.CC_NodeType==2)
                                {
                                    currentCause.MatchOrginalText = causeName.Item2;
                                    reList.Add(currentCause);
                                    break;
                                }
                            }

                        }
                    }
                }
            }
            // 返回案由实体列表
            return reList;
        }
    }
}
